package com.tange.ai.core.demo.add.lan;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.tange.ai.core.demo.R;
import com.tange.ai.core.demo.Utils;
import com.tange.ai.core.demo.databinding.ActivityAddLanScanBinding;
import com.tange.module.add.bind.DeviceBinding;
import com.tange.module.add.lan.DeviceLanSearch;
import com.tg.data.http.entity.BindDeviceBean;

import java.util.ArrayList;
import java.util.List;

public class DeviceLanScanActivity extends AppCompatActivity {

    private static final String TAG = "DeviceApScanActivity";

    private ActivityAddLanScanBinding binding;

    private LanDeviceListAdapter lanDeviceListAdapter;

    private DeviceLanSearch deviceLanSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityAddLanScanBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());


        lanDeviceListAdapter = new LanDeviceListAdapter();

        binding.apList.setAdapter(lanDeviceListAdapter);
        binding.apList.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));

        getSupportActionBar().hide();

        Utils.setStatusBar(this, Color.TRANSPARENT);

        binding.startScan.setOnClickListener(view -> startScan());

    }

    private void startScan() {
        deviceLanSearch =
                DeviceLanSearch.create(this, new DeviceLanSearch.Callback() {
                    @Override
                    public void onRefresh(List<String> deviceUuidList) {
                        // 每扫描到一个设备都会回调此方法
                        // 里面的字符串即为 UUID

                        lanDeviceListAdapter.refreshApList(deviceUuidList);
                    }

                    @Override
                    public void onSearchTimeout() {
                        // 超过预设时长，仍未扫描到设备
                        resetBtn();
                    }
                });

        deviceLanSearch.start();
        binding.startScan.setEnabled(false);
    }

    private void resetBtn() {
        binding.startScan.setText("重新扫描");
        binding.startScan.setEnabled(true);
        binding.startScan.setOnClickListener(view -> deviceLanSearch.start());
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (deviceLanSearch != null) {
            deviceLanSearch.stop();
        }
    }

    private void bindDevice(String uuid) {

        binding.statusTv.setText("尝试绑定设备：" + uuid);

        DeviceBinding.create().request(uuid, new DeviceBinding.Callback() {
            @Override
            public void onSuccess(BindDeviceBean bindDeviceBean) {
                binding.statusTv.setText("绑定成功，设备ID：" + bindDeviceBean.getId());
            }

            @Override
            public void onError(int code, String msg) {
                binding.statusTv.setText("绑定失败，code：" + code + " , msg = " + msg);
            }
        });
    }

    public class LanDeviceListAdapter extends RecyclerView.Adapter<ViewHolder> {

        private List<String> apList = new ArrayList<>();

        public void refreshApList(List<String> list) {
            this.apList.clear();
            this.apList.addAll(list);
            notifyDataSetChanged();
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_scan_ap, viewGroup, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder viewHolder, final int position) {
            final String uuid = apList.get(position);
            viewHolder.textView.setText(uuid);
            viewHolder.itemView.setOnClickListener(view -> {
                bindDevice(uuid);
            });
        }

        @Override
        public int getItemCount() {
            return apList.size();
        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public final TextView textView;

        public ViewHolder(View view) {
            super(view);
            textView = view.findViewById(R.id.ap_name);
        }
    }
}